<template>
  <a-layout class="user-layout">
    <a-layout-header class="header">
      <div class="header-left">
        <div class="logo-section">
          <div class="logo-icon">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M12 2L3 7V17L12 22L21 17V7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M12 22V12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              <path d="M21 7L12 12L3 7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
          </div>
          <div class="logo-text">
            <h2>心理思维训练系统</h2>
            <span class="logo-subtitle">Psychology Mind Training</span>
          </div>
        </div>
      </div>
      
      <div class="header-center">
        <a-dropdown trigger="click" position="bl" :popup-container="'body'">
          <a-button type="text" class="hotline-btn">
            <template #icon>
              <IconPhone />
            </template>
            <span class="hotline-text">心理援助热线</span>
          </a-button>
          <template #content>
            <div class="hotline-dropdown">
              <div class="hotline-list">
                <div class="hotline-item">
                  <span class="hotline-name">全国心理援助热线</span>
                  <a-button type="text" size="small" class="hotline-number" @click="copyHotline('12356')">
                    12356
                    <template #icon>
                      <IconCopy />
                    </template>
                  </a-button>
                </div>
                <div class="hotline-item">
                  <span class="hotline-name">青少年法律与心理咨询热线</span>
                  <a-button type="text" size="small" class="hotline-number" @click="copyHotline('12355')">
                    12355
                    <template #icon>
                      <IconCopy />
                    </template>
                  </a-button>
                </div>
                <div class="hotline-item">
                  <span class="hotline-name">全国心理卫生健康热线</span>
                  <a-button type="text" size="small" class="hotline-number" @click="copyHotline('12320')">
                    12320
                    <template #icon>
                      <IconCopy />
                    </template>
                  </a-button>
                </div>
                <div class="hotline-item">
                  <span class="hotline-name">北京市心理援助热线</span>
                  <a-button type="text" size="small" class="hotline-number" @click="copyHotline('800-810-1117')">
                    800-810-1117
                    <template #icon>
                      <IconCopy />
                    </template>
                  </a-button>
                </div>
                <div class="hotline-item">
                  <span class="hotline-name">希望24热线</span>
                  <a-button type="text" size="small" class="hotline-number" @click="copyHotline('400-161-9995')">
                    400-161-9995
                    <template #icon>
                      <IconCopy />
                    </template>
                  </a-button>
                </div>
                <div class="hotline-item">
                  <span class="hotline-name">武汉市"心心语"心理援助热线</span>
                  <a-button type="text" size="small" class="hotline-number" @click="copyHotline('027-85844666')">
                    027-85844666
                    <template #icon>
                      <IconCopy />
                    </template>
                  </a-button>
                </div>
                <div class="hotline-item">
                  <span class="hotline-name">华中师范大学心理援助热线</span>
                  <a-button type="text" size="small" class="hotline-number" @click="copyHotline('400-9678920')">
                    400-9678920
                    <template #icon>
                      <IconCopy />
                    </template>
                  </a-button>
                </div>
              </div>
      
            </div>
          </template>
        </a-dropdown>
      </div>
      
      <div class="header-right">
        <div class="user-info">
          <div class="user-avatar">
            <IconUser />
          </div>
          <div class="user-details">
            <span class="user-name">{{ userStore.user?.real_name || userStore.user?.username }}</span>
            <span class="user-progress">进度: 第{{ userStore.currentAllowNode }}章</span>
          </div>
        </div>
        <a-dropdown>
          <a-button type="text" class="user-menu-btn">
            <template #icon>
              <IconMore />
            </template>
          </a-button>
          <template #content>
            <a-doption @click="handleLogout">
              <template #icon>
                <IconPoweroff />
              </template>
              退出登录
            </a-doption>
          </template>
        </a-dropdown>
      </div>
    </a-layout-header>
    
    <a-layout-content class="content">
      <div class="content-wrapper">
        <router-view />
      </div>
    </a-layout-content>
  </a-layout>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { Modal, Message } from '@arco-design/web-vue'
import { 
  IconUser, 
  IconPoweroff,
  IconMore,
  IconPhone,
  IconCopy
} from '@arco-design/web-vue/es/icon'
import { useUserStore } from '../stores/user'
import { useQuestionStore } from '../stores/question'

const router = useRouter()
const userStore = useUserStore()
const questionStore = useQuestionStore()

const handleLogout = () => {
  Modal.confirm({
    title: '确认退出',
    content: '您确定要退出登录吗？当前的训练进度将会保存。',
    onOk: async () => {
      // 在退出前保存当前位置（如果在答题中）- 这属于异常退出
      if (userStore.user?.id && router.currentRoute.value.path.includes('/user/question/')) {
        await questionStore.saveCurrentPosition(userStore.user.id)
        console.log('💾 退出登录时已保存当前位置')
      }
      
      userStore.logout()
      router.push('/login')
    }
  })
}

// 复制热线电话到剪贴板
const copyHotline = async (phoneNumber: string) => {
  try {
    await navigator.clipboard.writeText(phoneNumber)
    Message.success(`已复制: ${phoneNumber}`)
  } catch (error) {
    console.error('复制失败:', error)
    Message.error('复制失败，请手动复制')
  }
}
</script>

<style scoped>
.user-layout {
  min-height: 100vh;
}

.header {
  background: linear-gradient(135deg, #4a90e2 0%, #357abd 100%);
  border-bottom: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 32px;
  height: 72px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 左侧Logo区域 */
.header-left {
  flex: 0 0 auto;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 12px;
  color: white;
  backdrop-filter: blur(10px);
}

.logo-icon svg {
  width: 24px;
  height: 24px;
}

.logo-text h2 {
  margin: 0;
  color: white;
  font-size: 18px;
  font-weight: 700;
  line-height: 1.2;
}

.logo-subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 11px;
  font-weight: 300;
  letter-spacing: 0.5px;
}

/* 中间导航区域 */
.header-center {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 援助热线按钮 */
.hotline-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 20px;
  color: white;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  animation: pulse 2s ease-in-out infinite;
}

.hotline-btn:hover {
  background: rgba(255, 255, 255, 0.25);
  border-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

@keyframes pulse {
  0%, 100% {
    box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.4);
  }
  50% {
    box-shadow: 0 0 0 8px rgba(255, 255, 255, 0);
  }
}

.hotline-text {
  color: white;
  font-weight: 600;
}

/* 援助热线下拉框 */
.hotline-dropdown {
  min-width: 340px;
  max-width: 90vw;
  padding: 16px;
  background: white;
  border-radius: 12px;
}

.hotline-title {
  font-size: 16px;
  font-weight: 600;
  color: #1d2129;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 2px solid #e5e6eb;
  text-align: center;
}

.hotline-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 50vh;
  overflow-y: auto;
  padding-right: 4px;
}

.hotline-list::-webkit-scrollbar {
  width: 6px;
}

.hotline-list::-webkit-scrollbar-track {
  background: #f2f3f5;
  border-radius: 3px;
}

.hotline-list::-webkit-scrollbar-thumb {
  background: #c9cdd4;
  border-radius: 3px;
}

.hotline-list::-webkit-scrollbar-thumb:hover {
  background: #a9aeb8;
}

.hotline-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  padding: 12px;
  background: #f7f8fa;
  border-radius: 8px;
  transition: all 0.2s ease;
}

.hotline-item:hover {
  background: #e8f3ff;
}

.hotline-name {
  font-size: 13px;
  color: #4e5969;
  font-weight: 500;
  line-height: 1.4;
  width: 100%;
}

.hotline-number {
  font-size: 15px;
  font-weight: 600;
  color: #165dff;
  font-family: 'Courier New', monospace;
  padding: 8px 12px;
  border-radius: 6px;
  transition: all 0.2s ease;
  width: 100%;
  justify-content: center;
  background: rgba(22, 93, 255, 0.05);
}

.hotline-number:hover {
  background: #165dff;
  color: white;
}

.hotline-tip {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #e5e6eb;
  font-size: 12px;
  color: #86909c;
  text-align: center;
  line-height: 1.5;
}

.nav-items {
  display: flex;
  gap: 8px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 6px;
  backdrop-filter: blur(10px);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: 8px;
  color: rgba(255, 255, 255, 0.8);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s ease;
  position: relative;
}

.nav-item:hover {
  color: white;
  background: rgba(255, 255, 255, 0.1);
}

.nav-item.active {
  color: white;
  background: rgba(255, 255, 255, 0.2);
}

.nav-icon {
  font-size: 16px;
  display: flex;
  align-items: center;
}

/* 右侧用户区域 */
.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
  flex: 0 0 auto;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  backdrop-filter: blur(10px);
}

.user-avatar {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  color: white;
  font-size: 16px;
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-name {
  color: white;
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
}

.user-progress {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
  line-height: 1;
}

.user-menu-btn {
  color: rgba(255, 255, 255, 0.8);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.user-menu-btn:hover {
  color: white;
  background: rgba(255, 255, 255, 0.1);
}

.content {
  background: #f8f9fa;
  min-height: calc(100vh - 72px);
}

.content-wrapper {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header {
    padding: 0 16px;
    height: 64px;
  }
  
  .logo-text h2 {
    font-size: 16px;
  }
  
  .logo-subtitle {
    display: none;
  }
  
  .header-center {
    flex: 0 0 auto;
    margin: 0 12px;
  }
  
  .hotline-text {
    display: none;
  }
  
  .hotline-btn {
    padding: 8px 12px;
    min-width: auto;
  }
  
  .user-details {
    display: none;
  }
  
  .user-info {
    padding: 8px;
  }
}

@media (max-width: 480px) {
  .logo-text {
    display: none;
  }
  
  .header {
    padding: 0 12px;
  }
  
  .hotline-dropdown {
    min-width: 280px;
    max-width: calc(100vw - 32px);
    padding: 12px;
  }
  
  .hotline-title {
    font-size: 15px;
    margin-bottom: 12px;
    padding-bottom: 10px;
  }
  
  .hotline-list {
    gap: 8px;
    max-height: 60vh;
  }
  
  .hotline-item {
    padding: 10px;
    gap: 6px;
  }
  
  .hotline-name {
    font-size: 12px;
    line-height: 1.3;
  }
  
  .hotline-number {
    font-size: 14px;
    padding: 8px 10px;
  }
  
  .hotline-tip {
    font-size: 11px;
    margin-top: 8px;
    padding-top: 8px;
  }
}
</style>
